package com.example.stock.service.impl;

import com.example.stock.excel.ExcelReaderUtil;
import com.example.stock.excel.StockCorr;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.StockCorrService;
import com.example.stock.service.WeekDataService;
import com.example.stock.util.DataUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StockCorrServiceImpl implements StockCorrService {

    @Autowired
    WeekDataService weekDataService;

    List<StockCorr> raw;

    @PostConstruct
    private void init() {
        raw = DataUtil.getListData(StockCorr.class, ExcelReaderUtil.FILE_OPERATION, "相关系数统计");
    }

    @Override
    public List<StockCorr> getStockCorrByStock(String stock) {
        List<StockCorr> ret = new ArrayList<>();
        raw.stream().filter(i -> i.getStock1().equals(stock)).forEach(ret::add);

        raw.stream().filter(i -> i.getStock2().equals(stock)).forEach(stockCorr -> {
            StockCorr item = new StockCorr();
            item.setStock1(stockCorr.getStock2());
            item.setStock2(stockCorr.getStock1());
            item.setCorr(stockCorr.getCorr());
            ret.add(item);
        });

        ret.forEach(i -> {
            WeekDetail currentWeekDetail = weekDataService.getCurrentWeekDetail(i.getStock2());
            i.setLp(currentWeekDetail.getLp());
        });
        return ret;
    }
}